<template>
  <div class="container">
    <div style="width: 400px; padding: 30px; background-color: white; border-radius: 5px;">
      <div style="text-align: center; font-size: 20px; margin-bottom: 20px; color: #333">欢迎注册</div>
      <el-form :model="form" :rules="rules" ref="formRef">
        <el-form-item prop="username">
          <el-input prefix-icon="el-icon-user" placeholder="请输入账号" v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input prefix-icon="el-icon-lock" placeholder="请输入密码" show-password  v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item prop="role" label="">
          <el-select v-model="form.userRole" clearable placeholder="用户角色">
            <el-option
                v-for="item in registerRole"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="confirmPass">
          <el-input prefix-icon="el-icon-lock" placeholder="请确认密码"  v-model="form.confirmPass"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button style="width: 100%; background-color: #333; border-color: #333; color: white" @click="doSubmit">注 册</el-button>
        </el-form-item>
        <div style="display: flex; align-items: center">
          <div style="flex: 1"></div>
          <div style="flex: 1; text-align: right">
            已有账号？请 <a href="/">登录</a>
          </div>
        </div>
      </el-form>
    </div>
  </div>
</template>
<script>
import axios from "axios";
import {roles} from "@/utils/select";
export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: "register" ,
  data () {
    const validatePassword = (rule, confirmPass, callback) => {
      if (confirmPass === '') {
        callback(new Error('请确认密码'))
      } else if (confirmPass !== this.form.password) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    }

    return {
      form:{
        username:'',
          password:'',
          userNumber:'',
          userRole:'',
          phoneNumber:'',
          confirmPass:''
    },
      rules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ],
        confirmPass: [
          { validator: validatePassword, trigger: 'blur' }
        ]
      },
      loginLoading: false,
      registerRole:roles
    }
  },
  created () {},
  methods: {
    doSubmit () {
        console.log(this.form.username)
        console.log(this.form.password)
        if (this.form.username === '' || this.form.password=== ''){
          this.$message.error('用户名或者学号或者密码不得存在空值！！！')
          return
        }
        //确认 第一次输入跟第二次输入的密码是否相等
        if (this.form.password !== this.form.confirmPass){
          this.$message.error("两次输入的密码不相等，请确认后输入！！")
          return;
        }

          let submitData = {
            userName:this.form.username,
            passWord:this.form.password,
            role:this.form.userRole
          }
          axios.post('http://localhost:4009/register', submitData).then(res => {
            console.log(res)
            if (res.data.status !== 200) {
              this.$message.error(res.data.message)
            } else {
              this.$message({
                message: '注册成功！！',
                type: 'success'
              })
            }

            // 开始跳转到 普通用户发帖页面
            console.log("返回登录界面")
            this.$router.push(`/`)

            this.form.username=''
            this.form.password=''
            this.form.userRole=''
            this.form.phoneNumber=''
            this.form.userNumber=''

          }).catch(error => {
            console.log(error)
            this.$message.error(error)
          })
        }
  }
}
</script>
<style scoped>
.container {
  height: 100vh;
  overflow: hidden;
  background-image: url("@/assets/img/bg.jpg");
  background-size: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #666;
}
a {
  color: #2a60c9;
}
</style>
